#include "xict_ui_core/imagemanage.h"
#include "xict_ui_common/utils/config.h"
#include "xict_ui_common/utils/msgsbridge.h"
#include <QDateTime>
#include <QPainter>
namespace xict_ui_core {

    ImageManager::ImageManager(QObject* parent) : QThread(parent) {
        init();
    }

    void ImageManager::init() {
        setfront_line_active(true);
        setback_line_active(true);
        setview_state(1);
        m_boot_url = QString::fromStdString(
            xict_ui_common::Config::GetInstance().GetYaml("ui",
                                                          "boot_animation"));
        m_cap = cv::VideoCapture(m_boot_url.toStdString());

        xict_ui_driver::ImageSdk::GetInstance().SetFrontImageCallback(
            std::bind(&ImageManager::FrontImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetFrontWheelImageCallback(
            std::bind(&ImageManager::FrontWheelImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetBackImageCallback(
            std::bind(&ImageManager::BackImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetLeftImageCallback(
            std::bind(&ImageManager::LeftImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetRightImageCallback(
            std::bind(&ImageManager::RightImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetFrontBladeImageCallback(
            std::bind(&ImageManager::FrontBladeImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetBackBladeImageCallback(
            std::bind(&ImageManager::BackBladeImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetOverLookImageCallback(
            std::bind(&ImageManager::OverLookImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetBladeMRImageCallback(
            std::bind(&ImageManager::BladeMRImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));

        xict_ui_driver::ImageSdk::GetInstance().SetBackLineImageCallback(
            std::bind(&ImageManager::BackLineImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
        xict_ui_driver::ImageSdk::GetInstance().SetFrontLineImageCallback(
            std::bind(&ImageManager::FrontLineImageCallback, this,
                      std::placeholders::_1, std::placeholders::_2));
    }

    void ImageManager::FrontImageCallback(const char*,
                                          const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        if (m_view_state == 1) {
            emit signalCurrentImage(m_view_state, qimg);
            return;
        }

        if (front_line_active()) {
            QMutexLocker locker(&mutex);   // 在访问共享资源之前加锁
            QPainter painter(&qimg);
            painter.drawImage(0, 630, m_front_line_image);
            painter.end();
        }
        switch (camera_changed()) {
        case 1:
            emit signalFrontImage(m_view_state, qimg);
            break;
        case 2:
            emit signalLeftImage(m_view_state, qimg);
            break;
        case 3:
            emit signalBackImage(m_view_state, qimg);
            break;
        case 4:
            emit signalRightImage(m_view_state, qimg);
            break;
        default:
            emit signalFrontImage(m_view_state, qimg);
            break;
        }
    }

    void ImageManager::BackImageCallback(const char*,
                                         const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        if (m_view_state == 2) {
            emit signalCurrentImage(m_view_state, qimg);
            return;
        }
        if (back_line_active()) {
            QMutexLocker locker(&mutex);   // 在访问共享资源之前加锁
            QPainter painter(&qimg);
            painter.drawImage(0, 570, m_back_line_image);
            painter.end();
        }
        switch (camera_changed()) {
        case 1:
            emit signalBackImage(m_view_state, qimg);
            break;
        case 2:
            emit signalBackImage(m_view_state, qimg);
            break;
        case 3:
            emit signalFrontImage(m_view_state, qimg);
            break;
        case 4:
            emit signalBackImage(m_view_state, qimg);
            break;
        default:
            emit signalBackImage(m_view_state, qimg);
            break;
        }
    }

    void ImageManager::LeftImageCallback(const char*,
                                         const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        if (m_view_state == 3) {
            emit signalCurrentImage(m_view_state, qimg);
            return;
        }
        switch (camera_changed()) {
        case 1:
            emit signalLeftImage(m_view_state, qimg);
            break;
        case 2:
            emit signalFrontImage(m_view_state, qimg);
            break;
        case 3:
            emit signalLeftImage(m_view_state, qimg);
            break;
        case 4:
            emit signalLeftImage(m_view_state, qimg);
            break;
        default:
            emit signalLeftImage(m_view_state, qimg);
            break;
        }
    }

    void ImageManager::RightImageCallback(const char*,
                                          const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        if (m_view_state == 4) {
            emit signalCurrentImage(m_view_state, qimg);
            return;
        }
        switch (camera_changed()) {
        case 1:
            emit signalRightImage(m_view_state, qimg);
            break;
        case 2:
            emit signalRightImage(m_view_state, qimg);
            break;
        case 3:
            emit signalRightImage(m_view_state, qimg);
            break;
        case 4:
            emit signalFrontImage(m_view_state, qimg);
            break;
        default:
            emit signalRightImage(m_view_state, qimg);
            break;
        }
    }

    void
    ImageManager::BackBladeImageCallback(const char*,
                                         const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        emit signalBackBladeImage(m_view_state, qimg);
        if (m_view_state == 5) {
            emit signalCurrentImage(m_view_state, qimg);
        }
    }

    void
    ImageManager::OverLookImageCallback(const char*,
                                        const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        emit signalOverLookImage(m_view_state, qimg);
        if (m_view_state == 6) {
            emit signalCurrentImage(m_view_state, qimg);
        }
    }

    void
    ImageManager::BladeMRImageCallback(const char*,
                                       const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        emit signalBladeMRImage(m_view_state, qimg);
    }

    void
    ImageManager::FrontLineImageCallback(const char*,
                                         const xcmg_proto::OpencvImage& img) {
        QMutexLocker locker(&mutex);
        m_front_line_image = xict_ui_common::ProtoImage2QImage(img);
        // emit signalLeftImage(m_view_state, m_front_line_image);
    }
    void
    ImageManager::BackLineImageCallback(const char*,
                                        const xcmg_proto::OpencvImage& img) {
        QMutexLocker locker(&mutex);
        m_back_line_image = xict_ui_common::ProtoImage2QImage(img);
        // emit signalRightImage(m_view_state, m_back_line_image);
    }
    void
    ImageManager::FrontWheelImageCallback(const char*,
                                          const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        // emit signalFrontWheelImage(m_view_state, qimg);
    }

    void
    ImageManager::FrontBladeImageCallback(const char*,
                                          const xcmg_proto::OpencvImage& img) {
        QImage qimg = xict_ui_common::ProtoImage2QImage(img);
        // emit signalFrontBladeImage(m_view_state, qimg);
    }

    void ImageManager::EnableSdk() {
        xict_ui_driver::ImageSdk::GetInstance().EnableFrontImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance().EnableBackImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance().EnableLeftImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance().EnableRightImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance()
            .EnableFrontLineImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance().EnableBackLineImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().EnableOverLookImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().EnableFrontBladeImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().EnableBackBladeImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().EnableFrontWheelImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().EnableBladeMRImageSubscriber();
    }
    void ImageManager::DisableSdk() {
        xict_ui_driver::ImageSdk::GetInstance().DisableFrontImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().DisableFrontWheelImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance().DisableBackImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance().DisableLeftImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance().DisableRightImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance()
            .DisableFrontLineImageSubscriber();
        xict_ui_driver::ImageSdk::GetInstance()
            .DisableBackLineImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().DisableFrontBladeImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().DisableBackBladeImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().DisableOverLookImageSubscriber();
        // xict_ui_driver::ImageSdk::GetInstance().DisableBladeMRImageSubscriber();
    }
    QImage scaleImage(const QImage& image, const QSize& newSize) {
        return image.scaled(newSize, Qt::IgnoreAspectRatio,
                            Qt::SmoothTransformation);
    }
    void ImageManager::run() {
        while (eCAL::Ok()) {
            if (m_view_state != -1) {
                continue;
            }
            // 获取系统当前时间
            QDateTime currentDateTime = QDateTime::currentDateTime();
            if (!m_cap.isOpened()) {
                if (!m_cap.open(m_boot_url.toStdString())) {
                    // qDebug() << "ERROR: open view fail " << m_boot_url;
                    continue;
                }
            }
            m_cap.read(m_frame);
            if (m_frame.rows > 0) {
                QImage image = xict_ui_common::CVMat2QImage(m_frame);
                // QSize newSize(1920, 1080); //
                // 假设我们要将图片的大小变为100x100 QImage image_front_line =
                // scaleImage(image, newSize); QImage image_back_line =
                // scaleImage(image, newSize);
                // // qDebug() << "c++ send  m_view_state" << m_view_state;
                emit signalCurrentImage(m_view_state, image);
                // if (front_line_active())
                // {
                //     QMutexLocker locker(&mutex); // 在访问共享资源之前加锁
                //     QPainter painter(&image_front_line);
                //     painter.drawImage(704, 284, m_front_line_image);
                //     painter.end();
                // }
                // emit signalFrontImage(m_view_state, image_front_line);
                // if (back_line_active())
                // {
                //     QMutexLocker locker(&mutex); // 在访问共享资源之前加锁
                //     QPainter painter(&image_back_line);
                //     painter.drawImage(704, 284, m_back_line_image);
                //     painter.end();
                // }
                // emit signalBackImage(m_view_state, image_back_line);
                // // emit signalFrontImage(m_view_state, image);
                // // emit signalBackImage(m_view_state, image);
                // emit signalLeftImage(m_view_state, image);
                // emit signalRightImage(m_view_state, image);
            } else {
                m_cap.release();
            }
        }
    }
}   // namespace xict_ui_core
